import uvicorn as uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
import json
from api import first_api
import  re

app = FastAPI()  # 必须实例化该类，启动的时候调用

txt2wav = first_api()

def hello(wav_path: str,text:str):
    print(wav_path,text)
    pattern = r',|\.|/|;|\'|`|\[|\]|<|>|\?|:|"|\{|\}|\~|!|@|#|\$|%|\^|&|\(|\)|-|=|\_|\+|，|。|、|；|‘|’|【|】|·|！| |…|（|）'
    result_list = re.split(pattern, text)
    print('-----------------',result_list)

    if len(text)<=20:
        filepath_get = txt2wav.run(wav_path, text)
        return filepath_get


    else:
        last_result = []
        str_ins = str()
        for tmp in result_list:
            str_ins += tmp
            if len(str_ins)+len(tmp) > 20:
                last_result.append(str_ins)
                str_ins = str()
        if len(str_ins)!=0:
            last_result.append(str_ins)
        filepath_get =[]

        for i in last_result:
            filepath_get.append(txt2wav.run(wav_path, i))

        return filepath_get


@app.get('/text/to/speech')
def fun(wav_path: str,text):
    path =  hello(wav_path,text)
    return path

if __name__ == '__main__':

    uvicorn.run(app=app, host="0.0.0.0", port=8000)


